package net.xo68.destiny.contract.domain.report;

import lombok.Data;
import lombok.ToString;
import net.xo68.destiny.contract.enums.handler.JobHandlePhaseEnum;
import net.xo68.destiny.contract.enums.handler.JobHandleStateEnum;

import java.io.Serializable;
import java.util.Date;

/**
 * 任务处理回复报文
 * @author wuxie
 * @version 2020-10-25
 */
@ToString
@Data
public class JobHandleReplyDatagram implements Serializable {

    /**
     * 构建者
     */
    public static class Builder{
        private long serialNumber;
        private long instanceId;
        private JobHandlePhaseEnum phase;
        private JobHandleStateEnum state;
        private Date start;
        private Date end;
        private String datagram;
        private String datagramClass;

        private final DatagramSerializer datagramSerializer;

        public Builder(DatagramSerializer datagramSerializer) {
            this.datagramSerializer=datagramSerializer;
            this.serialNumber=System.nanoTime();
        }

        public Builder instanceId(Long instanceId){
            this.instanceId=instanceId;
            return this;
        }
        public Builder phase(JobHandlePhaseEnum phase){
            this.phase=phase;
            return this;
        }
        public Builder state(JobHandleStateEnum state){
            this.state=state;
            return this;
        }
        public Builder start(Date start){
            this.start=start;
            return this;
        }
        public Builder end(Date end){
            this.end=end;
            return this;
        }

        public Builder datagram(Object datagram){
            //this.datagram=datagram;
            this.datagram=this.datagramSerializer.serialize(datagram);
            this.datagramClass=datagram.getClass().getName();
            return this;
        }
        public Builder init(Long instanceId,JobHandlePhaseEnum phase,JobHandleStateEnum state){
            this.instanceId=instanceId;
            this.phase=phase;
            this.state=state;
            return this;
        }
        public JobHandleReplyDatagram build(){
            return new JobHandleReplyDatagram(serialNumber,instanceId,phase,state,start,end,datagram,datagramClass);
        }
        public JobHandleReplyDatagram build(Object datagram){
            datagram(datagram);
            return build();
        }
    }
    public static Builder builder(DatagramSerializer datagramSerializer){
        return new Builder(datagramSerializer);
    }

    /**
     * 序列号，使用naos
     */
    private long serialNumber;
    /**
     * 任务实例编号
     */
    private long instanceId;

    /**
     * 上报阶段
     */
    private JobHandlePhaseEnum phase;
    /**
     * 状态
     */
    private JobHandleStateEnum state;

    /**
     * 开始时间
     */
    private Date start;
    /**
     * 结束时间
     */
    private Date end;
    /**
     * 报文内容
     */
    private String datagram;
    /**
     * 报文内容 class
     */
    private String datagramClass;

    public JobHandleReplyDatagram() {
    }

    public JobHandleReplyDatagram(long serialNumber, long instanceId, JobHandlePhaseEnum phase, JobHandleStateEnum state, Date start, Date end, String datagram) {
        this.serialNumber = serialNumber;
        this.instanceId = instanceId;
        this.phase = phase;
        this.state = state;
        this.start = start;
        this.end = end;
        this.datagram = datagram;
    }

    public JobHandleReplyDatagram(long serialNumber, long instanceId, JobHandlePhaseEnum phase, JobHandleStateEnum state, Date start, Date end, String datagram, String datagramClass) {
        this.serialNumber = serialNumber;
        this.instanceId = instanceId;
        this.phase = phase;
        this.state = state;
        this.start = start;
        this.end = end;
        this.datagram = datagram;
        this.datagramClass = datagramClass;
    }
}
